package com.supostacks.xuetanginterfaces.facade;

import com.alibaba.fastjson.JSONObject;
import com.supostacks.application.application.IBaseApplication;
import com.supostacks.domain.vo.BaseVo;
import com.supostacks.rpc.server.IDataFetchService;
import com.supostacks.rpc.server.req.FetchReq;
import com.supostacks.rpc.server.res.XResult;
import com.supostacks.xuetanginterfaces.factory.DefaultFactory;
import org.apache.dubbo.config.annotation.DubboService;

import java.util.List;

@DubboService
public class DataFetchImpl implements IDataFetchService {

    @Override
    public XResult fetch(FetchReq req) {
        XResult res = new XResult(200, "success");

        try {
            JSONObject option = req.getOption();

            IBaseApplication baseApplication = DefaultFactory.getApplication(req);
            assert baseApplication != null;
            String type = req.getType();
            List<BaseVo> list;
            switch (type) {
                case "selectById":
                    String id = option.getString("id");
                    res.setResult(baseApplication.selectById(id));
                    break;
                case "selectByPage":
                    Integer pageNo = option.getInteger("pageNo");
                    Integer pageSize = option.getInteger("pageSize");
                    res.setResult(baseApplication.selectByPage(option, pageNo, pageSize));
                    break;
                case "selectList":
                    option = new JSONObject();
                    list = baseApplication.selectList(option);
                    res.setResult(list);
                    break;
                case "search":
                    list = baseApplication.search(option);
                    res.setResult(list);
                    break;
            }
            return res;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
